package com.gri.saas;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gri.utils.GriUtils;

/**
 * Servlet implementation class CreateProject
 */
public class CreateProject extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateProject() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) {
		Connection con = null;
		try{
			
			String custId = request.getParameter("custId");
			String projectUrl = request.getParameter("purl");
			String projectKeywords = request.getParameter("pkeywords");
			String projectName = request.getParameter("pname");
			request.getSession().setAttribute("createProjectMessage", "");
			
			if(custId == null || custId.isEmpty()){
				/*
				 * If somehow cust id is blank, send the user to login page
				 */
				GriUtils.forwardToPage(request, response, "login.jsp");
			}else if(projectUrl == null || projectUrl.isEmpty() || projectKeywords == null || projectKeywords.trim().isEmpty() || projectName == null || projectName.isEmpty()){
				request.getSession().setAttribute("createProjectMessage", "Below fields are mandatory. Please provide the information");
				GriUtils.forwardToPage(request, response, "create_project.jsp");
			} else {
				con = GriUtils.getConnection();
				PreparedStatement st = con.prepareStatement("Insert Into TBL_PROJECT (cust_id,project_url,project_keywords,project_name) values(?,?,?,?)");
				st.setInt(1, Integer.parseInt(custId));
				st.setString(2, projectUrl);
				st.setString(3, projectKeywords.trim());
				st.setString(4, projectName);
				
				int retVal = st.executeUpdate();
				if(retVal !=1){
					request.getSession().setAttribute("createProjectMessage", "Could not create project because of some internal error.");
				} else {
					request.getSession().setAttribute("createProjectMessage", "Project created Successfully");
				}			
				con.close();
				
				GriUtils.forwardToPage(request, response, "create_project.jsp");
			}
		}catch(Exception ex){
			System.out.println("Exception occurred while creating project" + ex);
			request.getSession().setAttribute("createProjectMessage", "Could not create project because of some internal error.");
			GriUtils.forwardToPage(request, response, "create_project.jsp");
		}finally{
			if(con!=null){
				try{
					con.close();
				}catch(Exception ex){
					System.out.println("Exception occurred while creating project" + ex);
				}
			}
		}
		
	}

}
